package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/findAll")
    private List<User> findAll(User user){
        return userService.findAll(user);
    }

    @PostMapping("/login")
    private SysResult login(@RequestBody User user){
        String token = userService.login(user);

        if (token == null){
            return SysResult.fail();
        }
        return SysResult.success(token);
    }

    @GetMapping("/list")
    private SysResult getUserList(PageResult pageResult){
        return SysResult.success(userService.getUserList(pageResult));
    }

    @PutMapping("/status/{id}/{status}")
    public SysResult updateStatus(User user){
        userService.updateStatus(user);
        return SysResult.success();
    }

    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user){
        userService.addUser(user);
        return SysResult.success();
    }

    @GetMapping("/{id}")
    public SysResult findUserById(User user){
        return SysResult.success(userService.findUserById(user));
    }

    @PutMapping("/updateUser")
    public SysResult updateUser(@RequestBody User user){
        Integer rows = userService.updateUser(user);
        if (rows == 0)
            return SysResult.fail();
        //如果修改成功 把影响行数传到前端
        return SysResult.success(rows);
    }

    @DeleteMapping("/{id}")
    public SysResult delectUserById(@PathVariable Integer id){
        Integer rows = userService.delectUserById(id);
        if (rows == 0)
            return SysResult.fail();
        //如果删除成功 把影响行数传到前端
        return SysResult.success(rows);
    }
}
